package cn.pink.core.support;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Type;

/**
 *
 * @author Pink
 */
public class GsonUtil {
    public static final Gson gson = new Gson();

    /**
     * json化对象
     */
    public static String toJson(Object obj) {
        return gson.toJson(obj);
    }

    public static String toJson(Object obj, Class<?> type){
        return gson.toJson(obj, type);
    }

    public static <T> T fromJson(String str, Class<T> type) {
        return gson.fromJson(str, type);
    }

    public static <T> T fromJson(String json, Type typeOfT) {
        return gson.fromJson(json, typeOfT);
    }


    private static final Gson disGson = new GsonBuilder().disableHtmlEscaping().create();

    /**
     * json化对象
     */
    public static String toJsonDisableHtmlEsc(Object obj) {
        return disGson.toJson(obj);
    }

    public static String toJsonDisableHtmlEsc(Object obj, Class<?> type){
        return disGson.toJson(obj, type);
    }

    public static <T> T fromJsonDisableHtmlEsc(String str, Class<T> type) {
        return disGson.fromJson(str, type);
    }

    public static <T> T fromJsonDisableHtmlEsc(String json, java.lang.reflect.Type typeOfT) {
        return disGson.fromJson(json, typeOfT);
    }

}
